package ytmc.converter;

import java.awt.Dialog.ModalityType;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import ytmc.PanelView;


/**
 *
 * @author Kicok
 */
public class ConverterView extends PanelView implements TableModelListener
{
    private ConverterModel model;
    private ConverterController controller;

    private ConverterNewTaskView newTaskView;


    public ConverterView( ConverterController controller, ConverterModel model )
    {
        this.model = model;
        this.model.addTableModelListener( this );
        this.controller = controller;
        this.newTaskView = new ConverterNewTaskView( controller );

        initComponents();

        conversionTasksTable.setModel( model );
        conversionTasksTable.getTableHeader().setReorderingAllowed( false );
    }


    public void showMessage( String message )
    {
        JOptionPane.showMessageDialog( this, message );
    }


    public String getInputFilePath()
    {
        return newTaskView.getInputFilePath();
    }

    public void setInputFilePath( String path )
    {
        newTaskView.setInputFilePath( path );
    }

    public String getOutputFilePath()
    {
        return newTaskView.getOutputFilePath();
    }

    public void setOutputFilePath( String path )
    {
        newTaskView.setOutputFilePath( path );
    }

    public boolean isVideoConversion()
    {
        return newTaskView.isVideoConversion();
    }

    public boolean isAudioExtraction()
    {
        return newTaskView.isAudioExtraction();
    }


    @Override
    public void tableChanged( TableModelEvent tme )
    {
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        addTaskBtn = new javax.swing.JButton();
        removeTaskBtn = new javax.swing.JButton();
        startConversionBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        conversionTasksTable = new javax.swing.JTable();

        setMinimumSize(new java.awt.Dimension(600, 460));
        setPreferredSize(new java.awt.Dimension(600, 460));

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(1);
        jToolBar1.setRollover(true);
        jToolBar1.setMaximumSize(new java.awt.Dimension(42, 95));
        jToolBar1.setMinimumSize(new java.awt.Dimension(40, 2));
        jToolBar1.setPreferredSize(new java.awt.Dimension(33, 95));

        addTaskBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/assets/add24.png"))); // NOI18N
        addTaskBtn.setToolTipText("Dodaj zadanie konwersji");
        addTaskBtn.setFocusable(false);
        addTaskBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addTaskBtn.setMaximumSize(new java.awt.Dimension(40, 31));
        addTaskBtn.setMinimumSize(new java.awt.Dimension(40, 31));
        addTaskBtn.setPreferredSize(new java.awt.Dimension(31, 31));
        addTaskBtn.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        addTaskBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTaskBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(addTaskBtn);

        removeTaskBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/assets/delete24.png"))); // NOI18N
        removeTaskBtn.setToolTipText("Usuń zadanie konwersji");
        removeTaskBtn.setFocusable(false);
        removeTaskBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        removeTaskBtn.setMaximumSize(new java.awt.Dimension(40, 31));
        removeTaskBtn.setMinimumSize(new java.awt.Dimension(40, 31));
        removeTaskBtn.setPreferredSize(new java.awt.Dimension(31, 31));
        removeTaskBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        removeTaskBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeTaskBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(removeTaskBtn);

        startConversionBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/assets/play24.png"))); // NOI18N
        startConversionBtn.setToolTipText("Rozpocznij konwersje");
        startConversionBtn.setFocusable(false);
        startConversionBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        startConversionBtn.setMaximumSize(new java.awt.Dimension(40, 31));
        startConversionBtn.setMinimumSize(new java.awt.Dimension(40, 31));
        startConversionBtn.setPreferredSize(new java.awt.Dimension(40, 25));
        startConversionBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        startConversionBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startConversionBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(startConversionBtn);

        jScrollPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        conversionTasksTable.setAutoCreateRowSorter(true);
        conversionTasksTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, "", null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Plik", "Rodzaj zadania", "Status", "Rozmiar", "Postęp", "Pozostały czas"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        conversionTasksTable.setGridColor(new java.awt.Color(204, 204, 204));
        conversionTasksTable.setMaximumSize(new java.awt.Dimension(32767, 32767));
        conversionTasksTable.setMinimumSize(new java.awt.Dimension(23, 23));
        conversionTasksTable.setRowHeight(18);
        jScrollPane1.setViewportView(conversionTasksTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void removeTaskBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeTaskBtnActionPerformed
    {//GEN-HEADEREND:event_removeTaskBtnActionPerformed
        if( conversionTasksTable.getSelectedRowCount() > 0 )
        {
            controller.removeTasks( conversionTasksTable.getSelectedRows() );
        }
    }//GEN-LAST:event_removeTaskBtnActionPerformed

    private void addTaskBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addTaskBtnActionPerformed
    {//GEN-HEADEREND:event_addTaskBtnActionPerformed
        newTaskView.setModalityType( ModalityType.APPLICATION_MODAL );
        newTaskView.setVisible( true );
    }//GEN-LAST:event_addTaskBtnActionPerformed

    private void startConversionBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_startConversionBtnActionPerformed
    {//GEN-HEADEREND:event_startConversionBtnActionPerformed
        if( conversionTasksTable.getSelectedRowCount() > 0 )
        {
            controller.startTasks( conversionTasksTable.getSelectedRows() );
        }
    }//GEN-LAST:event_startConversionBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTaskBtn;
    private javax.swing.JTable conversionTasksTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton removeTaskBtn;
    private javax.swing.JButton startConversionBtn;
    // End of variables declaration//GEN-END:variables
}
